package cn.yu.takeoutbackend.employeeManagement.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.yu.takeoutbackend.common.PageSize;
import cn.yu.takeoutbackend.employeeManagement.dao.EmployeeManagementDao;
import cn.yu.takeoutbackend.employeeManagement.entity.Employee;
import cn.yu.takeoutbackend.employeeManagement.entity.EmployeeCondition;
import cn.yu.takeoutbackend.employeeManagement.entity.dto.EmployeeDto;
import cn.yu.takeoutbackend.employeeManagement.service.EmployeeManagementService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class EmployeeManagementServiceImpl extends ServiceImpl<EmployeeManagementDao,Employee> implements EmployeeManagementService {

    @Autowired
    private EmployeeManagementDao employeeManagementDao;

    private static final String DEFAULT_STATUS = "0";
    private static final String DEFAULT_PASSWORD = "0";
    @Override
    public void add(Employee employee) {
        employee.setId(IdUtil.getSnowflakeNextIdStr());
        employee.setPassword(DEFAULT_PASSWORD);
        employee.setStatus(DEFAULT_STATUS);
        try {
            final int i = employeeManagementDao.insert(employee);
            if (i == 1) {
                log.info("添加成功");
            } else {
                log.info("添加失败");
                throw new RuntimeException("添加失败");
            }
        }catch (Exception e){
            log.info("添加失败");
            e.printStackTrace();
        }

    }

    @Override
    public void delete(Employee employee) {
        final QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("account", employee.getAccount());
        final int i = employeeManagementDao.delete(wrapper);
        if (i == 1) {
            log.info("删除成功");
        } else {
            log.info("删除失败");
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public Page<Employee> getEmployeeListWithPage(EmployeeDto employeeDto) {
        final PageSize pageSize = employeeDto.getPageSize();
        final EmployeeCondition condition = employeeDto.getCondition();
        final Page<Employee> page = new Page<>(pageSize.getCurrentPage(), pageSize.getPageNum());
        return employeeManagementDao.selectPage(page, null);
    }

    /**
     * 根据状态查询员工
     * @param status
     * @return
     */
    @Override
    public List<Employee> selectEmployeeListByStatus(int status) {
        final QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("status",status);
        final List<Employee> employees = employeeManagementDao.selectList(wrapper);
        return employees;
    }


}
